#include "pch.hpp"
#include "StyleDocument.hpp"
#include "TextureAtlas.hpp"




void StyleDocument::load(const QDomElement& xml, const QDir& path)
{
	// First, we load the basic ui definition
	QDomNode ui = xml.namedItem("ui");
	if(ui.isNull())
		BOOSTEXT_THROW(exception("Missing element"));

	QDomElement uiElem = ui.toElement();

	m_texture = uiElem.attribute("file");
	m_size = int2(uiElem.attribute("w").toInt(), uiElem.attribute("h").toInt());

	QDomElement fontElem = xml.firstChildElement("font");
	while(!fontElem.isNull())
	{
		add(AdvancedFont(fontElem, path));

		fontElem = fontElem.nextSiblingElement("font");
	}

	QDomElement glyphElem = xml.firstChildElement("glyph");
	while(!glyphElem.isNull())
	{
		add(AdvancedGlyph(glyphElem, path));
		glyphElem = glyphElem.nextSiblingElement("glyph");
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void StyleDocument::save(QDomDocument& doc, QDomElement& xml, const QFileInfo& info)
{
	// The user chose "whatever.xml" so the image will be stored under "whatever.png"...
	QDir rootDir = info.absolutePath();
	m_texture = info.completeBaseName() + ".png";
	QString definitionFileName = info.fileName();
	QString completeImageFileName = rootDir.absoluteFilePath(m_texture);


	//
	// Atlas creation
	//

	QImage image = generateImage();
	QImageWriter w(completeImageFileName, "PNG");
	w.write(image);


	//
	// Serialization
	//

	
	QDomElement ui = doc.createElement("ui");
	ui.setAttribute("file", m_texture);
	ui.setAttribute("w", image.width());
	ui.setAttribute("h", image.height());

	xml.appendChild(ui);


	foreach(const AdvancedFont& font, m_fonts)
	{
		xml.appendChild(font.domElement(doc));
	}

	foreach(const AdvancedGlyph& glyph,m_glyphs)
	{
		xml.appendChild(glyph.domElement(doc));
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////

QImage StyleDocument::generateImage()
{
	Atlas::GlyphPlacements glyphs;
	foreach(AdvancedGlyph& glyph, m_glyphs)
		glyphs.push_back(GlyphPlacement(glyph));
	foreach(AdvancedGlyph& font, m_fonts)
		glyphs.push_back(GlyphPlacement(font));

	Atlas t(glyphs);
	return t.image();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

StyleDocument::Glyphs& StyleDocument::glyphs()
{
	return m_glyphs;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

const StyleDocument::Glyphs& StyleDocument::glyphs() const
{
	return m_glyphs;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

StyleDocument::Fonts& StyleDocument::fonts()
{
	return m_fonts;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

const StyleDocument::Fonts& StyleDocument::fonts() const
{
	return m_fonts;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
